package com.geekhealth.bmi;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BMIServlet
 */
public class BMIServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BMIServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OutputStream out = response.getOutputStream();
		this.writeForm(out, "", "", "", "");
	}

	protected void writeForm(OutputStream out, String weight, String height, String weightError, String heightError) throws IOException {
		out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>".getBytes());
		out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">".getBytes());
		out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">".getBytes());
		out.write("<head>".getBytes());
		out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />".getBytes());
		out.write("<title>Please enter your height and weight so we can compute your BMI</title>".getBytes());
		out.write("</head>".getBytes());
		out.write("<body>".getBytes());
		out.write("<form method=\"post\" action=\"/GeekHealth/bmi\">".getBytes());
		out.write(("Height in Inches: <input type=\"text\" name=\"height\" value=\"" + height + "\"/> " + heightError + "<br/>").getBytes());
		out.write(("Weight in Inches: <input type=\"text\" name=\"weight\" value=\"" + weight + "\"/> " + weightError + "<br/>").getBytes());
		out.write("<input type=\"submit\" value=\"Calculate Your BMI\"/>".getBytes());
		out.write("</form>".getBytes());
		out.write("</body>".getBytes());
		out.write("</html>".getBytes());	
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		OutputStream out = response.getOutputStream();

		Float heightVal = null;
		Float weightVal = null;
		String height = request.getParameter("height");
		String weight = request.getParameter("weight");
		String heightError = "";
		String weightError = "";
		
		if ((height == null) || (height.length() == 0))
				heightError = "You must supply a hight in inches";
		
		if ((weight == null) || (weight.length() == 0))
				weightError = "You must supply a weight in pounds";

		if (heightError.length() == 0) {
			try {
				heightVal = Float.valueOf(height);
			}
			catch (Exception e) {
				heightError = "The hight must be a number";
			}
		}
		if (weightError.length() == 0) {
			try {
				weightVal = Float.valueOf(weight);			
			}
			catch (Exception e) {
				weightError = "The weight must be a number";
			}
		}
		
		if ((weightError.length() + heightError.length()) > 0) {
			this.writeForm(out, weight, height, weightError, heightError);
			return;
		}
		
		Float bmi = (weightVal / (heightVal * heightVal)) * 703;

		String result = "<html><head><title>You BMI Index</title>"
			+ "</head><body><h1>You BMI Index is " + bmi
			+ "</body></html>";

		out.write(result.getBytes());
	}
}
